﻿// Less2DB.NET framework
// http://code.google.com/p/less2db/
//
// Copyright © Wangzhongwu, 2011
// alex.wzw@gmail.com
//

namespace Less2DB.SQLBuilder
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// 排序条件。
    /// </summary>
    class SQLOrderBy
    {
        public enum SortDirection
        {
            ASC,
            DESC
        }

        private List<string> _fields = new List<string>();

        /// <summary>
        /// 排序条件字段。
        /// </summary>
        public List<string> Fields
        {
            get { return _fields; }
        }

        private SortDirection _direction;

        /// <summary>
        /// 获取或设置排序方向。
        /// </summary>
        public SortDirection Direction
        {
            get { return _direction; }
            set { _direction = value; }
        }

        /// <summary>
        /// 获取排序字符串。
        /// </summary>
        public string OrderByString
        {
            get
            {
                StringBuilder sb = new StringBuilder();

                foreach (string field in this.Fields)
                {
                    if (sb.Length == 0)
                        sb.Append("ORDER BY " + field);
                    else
                        sb.Append(", " + field);
                }

                if (sb.Length > 0)
                    sb.Append(" " + this.Direction.ToString());

                return sb.ToString();
            }
        }
    }
}
